package com.xiaomi.front.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.front.service.address.AddressService;
import com.xiaomi.front.service.bill.BillService;
import com.xiaomi.front.service.mycollection.MycollectionService;
import com.xiaomi.front.service.user.UserService;
import com.xiaomi.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FirstController {

    @Autowired
    private MycollectionService mycollectionService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressService addressService;

    @Autowired
    private BillService billService;

    //跳转隐私政策页
    @RequestMapping("userPrivacy")
    public String userPrivacy() {
        return "front/userPrivacy";
    }

    //跳转用户协议页
    @RequestMapping("userProtocol")
    public String userProtocol() {
        return "front/userProtocol";
    }


    //跳转个人中心页
    //显示信息
    @RequestMapping("/Center")
    public String Center(HttpSession session, Model model) {

        //获取个人信息
        User LoginUser = (User) session.getAttribute("user");

        if (LoginUser == null) {
            return "forward:/front_login.jsp";
        } else {
            //传值
            model.addAttribute("loginuser", LoginUser);

            //喜欢商品得数量
            int count = mycollectionService.GetCount(LoginUser.getUid());
            model.addAttribute("count", count);

            //待支付订单数量
            int Unpaid=billService.getBillCount(LoginUser.getUid(),1);
            model.addAttribute("Unpaid", Unpaid);

            //待收货订单数量
            int received=billService.getBillCount(LoginUser.getUid(),3);
            model.addAttribute("received",received);

            //待评论
            int comment=billService.getBillCount(LoginUser.getUid(),4);
            model.addAttribute("comment",comment);

            return "front/Center/Center";
        }
    }

    //根据uid 查看收藏信息
    @RequestMapping("/GetColl")
    public String GetColl(@RequestParam(defaultValue = "1") int pageinfo, Model model, HttpSession session) {
        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "front/index";
        } else {

            //设置当前页码和几条数据
            PageHelper.startPage(pageinfo, 6);
            List<Mycollection> mycollectionList = mycollectionService.GetAllColl(loginUser.getUid());
            PageInfo<Mycollection> page = new PageInfo<Mycollection>(mycollectionList);
            model.addAttribute("page", page);

            return "front/Center/coll";
        }
    }

    //ajax 删除收藏
    // 分页回显
    @RequestMapping(value = "/Ajax_removeColl", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String removeColl(@RequestParam("deid") int deid, @RequestParam(defaultValue = "1") int pageinfo, Model model, HttpSession session) {
        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "未登录";
        } else {
            //删除
            int count = mycollectionService.deleteCollection(loginUser.getUid(), deid);
            //重新获取分页
            //设置当前页码和几条数据
            PageHelper.startPage(pageinfo, 6);
            List<Mycollection> mycollectionList = mycollectionService.GetAllColl(loginUser.getUid());
            PageInfo<Mycollection> page = new PageInfo<Mycollection>(mycollectionList);
            model.addAttribute("page", page);

            String json = JSON.toJSONString(page);
            return json;


        }
    }

    //跳转个人信息页
    @RequestMapping("/GoPersonal")
    public String GoPersonal(HttpSession session) {

        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "forward:/front_login.jsp";
        } else {
            return "front/Center/PersonalCenter";
        }
    }

    //修改密码页
    @RequestMapping("/UpdatePass")
    public String UpdatePass(HttpSession session) {
        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "forward:/front_login.jsp";
        } else {
            return "front/Center/changpassword";
        }
    }

    //修改密码
    @RequestMapping("/ajax_UpdatePass")
    @ResponseBody
    public String ajax_UpdatePass(String password1, String password, HttpSession session, Model model) {
        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "未登录";
        } else {
            String phone = loginUser.getPnumber();
            int count = userService.UpdatePass(password1, password, phone);

            if (count == 1) {
                return "修改成功！";
            } else {
                return "修改失败！";
            }
        }
    }

    //修改个人信息
    @RequestMapping("/ajax_Updateinfo")
    @ResponseBody
    public String ajax_Updateinfo(String name, String sex, String birthday, HttpSession session, Model model) {

        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "未登录";
        } else {
            int uid = loginUser.getUid();
            User user = new User();
            user.setUid(uid);
            user.setBirthday(birthday);
            user.setUname(name);
            user.setSex(sex);

            int count = userService.Updateinfo(user);
            //获取session 的账户 密码
            String phone = loginUser.getPnumber();
            String pass = loginUser.getPassword();
            if (count == 1) {
                //移除session
                session.removeAttribute("user");
                //跟新session
                session.setAttribute("user", userService.DefaultLogin(phone, pass));
                return "修改成功！";
            } else {
                return "修改失败！";
            }
        }
    }

    //跳转收货地址
    @RequestMapping("/Address")
    public String GoAddress(HttpSession session, Model model) {

        User loginUser = (User) session.getAttribute("user");

        if (loginUser == null) {
            return "forward:/front_login.jsp";
        } else {
            List<Address> addressList = addressService.addList(loginUser.getUid());
            model.addAttribute("addressList", addressList);
            return "front/Center/address";
        }
    }


    //删除地址
    //根据uid aid 修改表状态
    @RequestMapping("/ajax_DelAddress")
    @ResponseBody
    public int DelAddress(int aid, HttpSession session) {

        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return 0;
        } else {
            int uid = loginUser.getUid();
            int count = addressService.UpdateState(aid, uid);
            return count;
        }
    }

    //修改地址
    @RequestMapping("/ajax_UpAddress")
    @ResponseBody
    public int UpAddress(String name, String tel, String addres, int aid, HttpSession session) {

        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return 0;
        } else {
            Address address = new Address();
            address.setAid(aid);
            address.setAname(name);
            address.setAddress(addres);
            address.setUid(loginUser.getUid());
            address.setPhonenum(tel);

            int count = addressService.UpdateAddress(address);
            return count;
        }
    }

    //添加地址
    @RequestMapping("/ajax_addAddress")
    @ResponseBody
    public int addAddress(String name, String tel, String addres, HttpSession session) {

        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return 0;
        } else {
            Address address = new Address();
            address.setAname(name);
            address.setAddress(addres);
            address.setUid(loginUser.getUid());
            address.setPhonenum(tel);

            int count = addressService.addAddress(address);
            return count;
        }
    }


    //个人中心---全部订单
    @RequestMapping("/Order_center")
    public String Order_center(@RequestParam(defaultValue = "1") int currentPageNo, Model model, HttpSession session){

        User loginUser=(User) session.getAttribute("user");
        if(loginUser==null){
            return "forward:/front_login.jsp";
        }else{
            int uid=loginUser.getUid();

            //发页 查订单信息
            PageHelper.startPage(currentPageNo, 2);
            List<Bill> billdetailedList=billService.getAll(uid,5);
            for(Bill bill:billdetailedList){
                System.out.println("订单编号"+bill.getBid());
                System.out.println(bill.getBilldetailedList().toString());
            }
            PageInfo<Bill>page=new PageInfo<>(billdetailedList);
            model.addAttribute("page", page);
            return "front/Center/OrderList";
        }
    }

    //个人中心---已完成订单
    @RequestMapping("/Order_complete")
    public String Order_complete(@RequestParam(defaultValue = "1") int currentPageNo, Model model, HttpSession session){

        User loginUser=(User) session.getAttribute("user");
        if(loginUser==null){
            return "forward:/front_login.jsp";
        }else{
            int uid=loginUser.getUid();

            //发页 查订单信息
            PageHelper.startPage(currentPageNo, 2);
            List<Bill> billdetailedList=billService.getAll(uid,4);

            PageInfo<Bill>page=new PageInfo<>(billdetailedList);
            model.addAttribute("page", page);
            return "front/Center/Order_complete";
        }
    }

    //个人中心---待支付订单
    @RequestMapping("/Order_Unpaid")
    public String Order_Unpaid(@RequestParam(defaultValue = "1") int currentPageNo, Model model, HttpSession session){

        User loginUser=(User) session.getAttribute("user");
        if(loginUser==null){
            return "forward:/front_login.jsp";
        }else{
            int uid=loginUser.getUid();

            //发页 查订单信息
            PageHelper.startPage(currentPageNo, 2);
            List<Bill> billdetailedList=billService.getAll(uid,1);

            PageInfo<Bill>page=new PageInfo<>(billdetailedList);
            model.addAttribute("page", page);
            return "front/Center/Order_Unpaid";
        }
    }


    //个人中心---待收货订单
    @RequestMapping("/Order_received")
    public String Order_received(@RequestParam(defaultValue = "1") int currentPageNo, Model model, HttpSession session){

        User loginUser=(User) session.getAttribute("user");
        if(loginUser==null){
            return "forward:/front_login.jsp";
        }else{
            int uid=loginUser.getUid();

            //发页 查订单信息
            PageHelper.startPage(currentPageNo, 2);
            List<Bill> billdetailedList=billService.getAll(uid,3);

            PageInfo<Bill>page=new PageInfo<>(billdetailedList);
            model.addAttribute("page", page);
            return "front/Center/Order_received";
        }
    }
    //个人中心---按单号模糊查询
    @RequestMapping("/getBid")
    public String getOrder(@RequestParam(defaultValue = "1") int currentPageNo, String keywords, Model model, HttpSession session) {

        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return "forward:/front_login.jsp";
        } else {
            //发页 查订单信息
            List<Bill> page = billService.getBid(loginUser.getUid(), keywords);


            model.addAttribute("page", page);
            return "front/Center/orderSeach";
        }
    }



    //跳转个人信息编辑
    @RequestMapping("/Update")
    public String Update(HttpSession session) {
        User loginUser = (User) session.getAttribute("user");
        if (loginUser == null) {
            return "redirect:front_login.jsp";
        } else {
            return "front/Center/information";
        }
    }





}
